package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalUserCommodityService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AdditionalUserCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/10.
 */
public class ManageUserCommodityAction extends BaseAction<AdditionalUserCommodityModel> implements ModelDriven<AdditionalUserCommodityModel> {

    @Resource
    private AdditionalUserCommodityService additionalUserCommodityService;

    private Map<String, Object> data;

    /**
     * 进入页面
     *
     * @return
     */
    @Action("/modules/additional/commodity/userCommodityManage")
    public String userCommodityManage() {
        return "userCommodityManage";
    }

    /**
     * 用户获得物品列表
     *
     * @throws IOException
     */
    @Action("/modules/additional/commodity/userCommodityList")
    public void userCommodityList() throws IOException {
        data = new HashMap<String, Object>();
        //防止基本类型初始化0
        String status = paramString("status");
        if (StringUtil.isBlank(status))
            model.setStatus(99);
        PageDataList<AdditionalUserCommodityModel> pageDataList = additionalUserCommodityService.list(model);
        data.put("total", pageDataList.getPage().getTotal());
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 详情页面
     *
     * @throws Exception
     */
    @Action("/modules/additional/commodity/userCommodityDetailPage")
    public String userCommodityDetailPage() throws Exception {
        data = new HashMap<String, Object>();
        AdditionalUserCommodityModel additionalUserCommodityModel = additionalUserCommodityService.findById(model);
        request.setAttribute("userCommodity", additionalUserCommodityModel);
        return "userCommodityDetailPage";
    }

    /**
     * 审核页面
     *
     * @throws Exception
     */
    @Action("/modules/additional/commodity/userCommodityAuditPage")
    public String userCommodityAuditPage() throws Exception {
        data = new HashMap<String, Object>();
        AdditionalUserCommodityModel additionalUserCommodityModel = additionalUserCommodityService.findById(model);
        int ipCount = additionalUserCommodityService.SingleIpCount(additionalUserCommodityModel.getAddIp());
        request.setAttribute("userCommodity", additionalUserCommodityModel);
        request.setAttribute("ipCount", ipCount);
        return "userCommodityAuditPage";
    }

    /**
     * 审核
     *
     * @throws Exception
     */
    @Action("/modules/additional/commodity/doUserCommodityAudit")
    public void doUserCommodityAudit() throws Exception {
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        //设置审核人
        model.setAuditOperator(operator);
        int result = additionalUserCommodityService.doAudit(model);
        if (result <= 0) {
            printWebResult("审核更新失败！", false);
            return;
        }
        printWebSuccess();
    }

    /**
     * 发货页面
     *
     * @return
     */
    @Action("/modules/additional/commodity/userCommodityPostPage")
    public String userCommodityPostPage() {
        data = new HashMap<String, Object>();
        AdditionalUserCommodityModel additionalUserCommodityModel = additionalUserCommodityService.findById(model);
        request.setAttribute("userCommodity", additionalUserCommodityModel);
        return "userCommodityPostPage";
    }

    /**
     * 发货操作
     */
    @Action("/modules/additional/commodity/doUserCommodityPost")
    public void doUserCommodityPost() throws IOException {
        if (null == model.getExpressName() || null == model.getExpressNum()) {
            printWebResult("快递信息不完全！", false);
            return;
        }
        Operator operator = getOperator();
        if (null == operator) {
            printWebResult("请先登录！", false);
            return;
        }
        model.setPostOperator(operator);
        int result = additionalUserCommodityService.doPost(model);
        if (result <= 0) {
            printWebResult("发货操作失败！可能因为商品发放数量大于总数量", false);
            return;
        }
        printWebSuccess();
    }


}
